home *** CD-ROM | disk | FTP | other *** search
- Path: nuscc.nus.sg!satrajit
- From: satrajit@iscs.nus.sg (Satrajit Sujit Ghosh)
- Newsgroups: comp.lang.c++
- Subject: Is RTTI required if polymorphic constructor exists
- Date: 13 Apr 1996 06:20:24 GMT
- Organization: National University of Singapore
- Message-ID: <4knh38$lbl@nuscc.nus.sg>
- NNTP-Posting-Host: satrajit@sununx.iscs.nus.sg
- X-Newsreader: TIN [version 1.2 PL2]
-
-
- Hi,
- The following is a piece of code for run-time object registry
- without the use of the RTTI library or templates. It should be pretty
- simple to decipher. It creates a polymorphic constructor, without
- specifying a case statement, using static initializers.
- Given this situation of object identification, is RTTI really necessary ?
-
- Satra[jit]
- ------------------------------------------------------
- http://www.iscs.nus.sg/~satrajit
- Department of Information Systems and Computer Science
- National University of Singapore
- ======================================================
-
-
- /*
- /////////////////THE POLYMORPHIC CONSTRUCTOR///////////////////
- The following code segments demonstrate the creation of a polymorphic
- constructor.
- ///////////////////////////////////////////////////////////////
- */
-
- #include <iostream.h>
- #include <string.h>
-
- template<class T>
- class Clist{
- public:
- Clist(){
- i = 0;
- };
-
- void insert(char *name,T *(*func)()){
- for(int j=0;j<i;j++)
- // prevent re-registration
- if (!strcmp(nm_arr[j],name)){
- return;
- }
-
- nm_arr[i] = strdup(name);
- fun[i] = func;
- i++;
- }
-
- T* get(char *name){
- int j;
- for(j=0;j<i;j++)
- if (!strcmp(nm_arr[j],name)){
- return (*fun[j])();
- }
- return NULL;
- };
-
- // currently can support 5 derived classes
- char *nm_arr[5];
- T *(* fun[5])();
- int i;
- };
-
-
-
- class CShape{
- public:
- virtual void print(){
- cout << "reg" << endl;
- };
-
- static CShape * mk_subclass(char *str){
- return reg.get(str);
- }
-
- protected:
- static int reg_sub(char *name,CShape *(*func)()){
- reg.insert(name,func);
- return 0;
- };
-
- private:
- static Clist<CShape> reg;
-
- };
-
- Clist<CShape> CShape::reg;
-
- class CCircle:public virtual CShape{
- public:
- virtual void print(){
- cout <<"CCircle";
- };
-
- static CShape* int_new(){
- CShape *p = new CCircle;
- return p;
- };
- private:
- static int i;
- };
-
- int CCircle::i = reg_sub("CCircle",&CCircle::int_new);
-
- class CRect:public virtual CShape{
- public:
-
- virtual void print(){
- cout <<"CRect";
- };
-
- static CShape* int_new(){
- CShape *p = new CRect;
- return p;
- };
-
- private:
- static int i;
- };
-
- int CRect::i = reg_sub("CRect",&CRect::int_new);
-
- class CSquare:public virtual CRect{
- public:
-
- virtual void print(){
- cout <<"CSquare";
- };
-
- static CShape* int_new(){
- CShape *p = new CSquare;
- return p;
- };
-
- private:
- static int i;
- };
-
- int CSquare::i = reg_sub("CSquare",&CSquare::int_new);
-
-
- int
- main(){
- CShape *fg = CShape::mk_subclass("CCircle");
- cout << "Testing constructed CCircle [";
- fg->print();
- cout << "]\n";
- CShape *fg1 = CShape::mk_subclass("CRect");
- cout << "Testing constructed CRect [";
- fg1->print();
- cout << "]\n";
- CShape *fg2 = CShape::mk_subclass("CSquare");
- cout << "Testing constructed CSquare [";
- fg2->print();
- cout << "]\n";
- return 0;
- }
-
- --
- Satra[jit]
- ------------------------------------------------------
- http://www.iscs.nus.sg/~satrajit
- Department of Information Systems and Computer Science
- National University of Singapore
- ======================================================
-